package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.FoodOrderInfosBean;
import com.server.llh.llhserverwork.port.FoodOrderInfosInterface;
import com.server.llh.llhserverwork.utils.ListViewHeightUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ZTF on 2017-06-15.
 */

public class FoodOrderInfoHolder extends BaseAdapter {
    FoodOrderInfosInterface foodOrderInfosInterface;
    List<FoodOrderInfosBean.DataBean> foodOrderInfoList;
    Context context;

    public FoodOrderInfoHolder(Context context, List<FoodOrderInfosBean.DataBean> foodOrderInfoList) {
        this.context = context;
        this.foodOrderInfoList = foodOrderInfoList;
    }

    public void registBack(FoodOrderInfosInterface foodOrderInfosInterface) {
        this.foodOrderInfosInterface = foodOrderInfosInterface;
    }

    @Override
    public int getCount() {
        if (foodOrderInfoList == null && foodOrderInfoList.size() == 0) {
            return 0;
        }
        return foodOrderInfoList.size();

    }

    @Override
    public Object getItem(int position) {
        return foodOrderInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder1 = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.llh_f_order_view, null);
            holder1 = new Holder();
            holder1.orderNum = (TextView) convertView.findViewById(R.id.order_num);
            holder1.orderDownTime = (TextView) convertView.findViewById(R.id.order_down_time);
            holder1.orderPaystatus = (TextView) convertView.findViewById(R.id.order_paystatus);
            holder1.orderMoney = (TextView) convertView.findViewById(R.id.order_money);
            holder1.orderYouhui = (TextView) convertView.findViewById(R.id.order_youhui);
            holder1.orderPayMoney = (TextView) convertView.findViewById(R.id.order_pay_money);
            holder1.goPay = (TextView) convertView.findViewById(R.id.go_pay);
            holder1.goQuitOrder = (TextView) convertView.findViewById(R.id.go_quit_order);
            holder1.food_info = (ListView) convertView.findViewById(R.id.food_info);
            convertView.setTag(holder1);

        } else {
            holder1 = (Holder) convertView.getTag();
        }
        final FoodOrderInfosBean.DataBean foodOrderData = foodOrderInfoList.get(position);
        holder1.orderNum.setText("订单编号：" + foodOrderData.getOrderCode());
        holder1.orderDownTime.setText("下单时间" + foodOrderData.getAddTime());
        holder1.orderPayMoney.setVisibility(View.INVISIBLE);
        if (foodOrderData.getPayStatus() == 0) {
            holder1.orderPaystatus.setText("未支付");
        } else if (foodOrderData.getPayStatus() == 1) {
            holder1.orderPaystatus.setText("已支付");
        } else if (foodOrderData.getPayStatus() == 2) {
            holder1.orderPaystatus.setText("退款成功");
        } else if (foodOrderData.getPayStatus() == 3) {
            holder1.orderPaystatus.setText("退款处理中");
        }
        if ((Constants.ORDER_WAITE_CONSUME + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("待客户就餐");
        } else if ((Constants.ORDER_WAIT_SEND_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("待配送");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_SENDING_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("配送中");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_SEND_OVER_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("配送成功");
        } else if ((Constants.ORDER_SUCESS_OVER_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("交易成功");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_CLOSE_OVER_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("交易关闭");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_CANCEL_SUCCESS_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("订单已取消");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_REFUNDING_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("退款中");
            holder1.goPay.setVisibility(View.GONE);
        } else if ((Constants.ORDER_REFUND_OVER_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.orderPaystatus.setText("退款成功");
            holder1.goPay.setVisibility(View.GONE);
        }
        OrderInfoAdapter orderInfoAdapter = new OrderInfoAdapter(context, foodOrderData.getWapDietInfoList());
        holder1.food_info.setAdapter(orderInfoAdapter);
        ListViewHeightUtil.setListViewHeightBasedOnChildren(holder1.food_info);
        holder1.orderMoney.setText("订单金额：¥ " + foodOrderData.getTotalMoney());
        holder1.orderYouhui.setText("优惠：¥ 0.00");
        holder1.goPay.setText("去支付");

        holder1.goPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        foodOrderInfosInterface.reqData(foodOrderData);
            }
        });
        if ((Constants.ORDER_SUCESS_OVER_STATUS + "").equals(foodOrderData.getOrderStatus())) {
            holder1.goQuitOrder.setText("确认收货");
            holder1.goQuitOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    foodOrderInfosInterface.updateData(foodOrderData);
                }
            });
        }
        return convertView;
    }

    class Holder {
        TextView orderNum, orderDownTime, orderPaystatus, orderMoney, orderYouhui, orderPayMoney,
                goPay, goQuitOrder;
        ListView food_info;
    }


}
